/**
 * 
 */
package com.ccb.channel.p8;

import java.io.IOException;
import java.sql.Timestamp;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.ccb.channel.atm.AtmMapper;
import com.ccb.filter.Filters;
import com.ccb.filter.RecordFilter;
import com.ccb.util.CTool;
import com.ccb.util.DateUtil;
import com.ccb.util.Record;
import com.ccb.util.RecordType;
import com.ccb.util.XmlJSON;

/**
 * @author congyang
 *
 */
public class P8Mapper extends Mapper<LongWritable, Text, Text, Text> {
	private static final Logger logger = Logger.getLogger(AtmMapper.class);
	private RecordFilter recordFilter;
	private String timePattern = "[\\w\\W]*\\[([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{3})\\][\\w\\W]*";

	@Override
	protected void setup(Context context) throws IOException, InterruptedException {
		recordFilter = Filters.getFilter("p8");
		if (recordFilter == null) {
			logger.info("fiter is null");
		}
	}

	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		String allvalue = value.toString().trim();
		String recordValue = "";
		String headInfo = "";
		String serviceId = "";
		String matchKey = "";
		Record record = new Record();
		try {
			if(allvalue.isEmpty())return;
			headInfo = allvalue.substring(0, allvalue.indexOf("<?xml"));
			recordValue = allvalue.substring(allvalue.indexOf("<?xml"));

			matchKey = StringUtils.substringBetween(recordValue, "<SYS_EVT_TRACE_ID>", "</SYS_EVT_TRACE_ID>");
			matchKey = CTool.removeCData(matchKey);
			serviceId = StringUtils.substringBetween(recordValue, "<SYS_TX_CODE>", "</SYS_TX_CODE>");
			serviceId = CTool.removeCData(serviceId);
			if (recordFilter != null && !recordFilter.accept(serviceId)) {
				return;
			}
			if (matchKey.isEmpty() || serviceId.isEmpty())
				return;

				record.setServiceId(serviceId);
				record.setUid(matchKey);
				record.setOriginalData(recordValue);
				record.setData(XmlJSON.xmlToJSONObj(recordValue));
				if (recordValue.indexOf("<cmdstatus>") > -1) {
					String responseCode = StringUtils.substringBetween(recordValue, "<SYS_RESP_CODE>", "</SYS_RESP_CODE>");
					record.setResponseCode(responseCode);
					record.setRecordType(RecordType.RESPONSE);
				} else {
					record.setRecordType(RecordType.REQUEST);
					String timeStr = CTool.findStrForPattern(allvalue, timePattern);
					String formatStr = DateUtil.date2Str("yyyy-MM-dd HH:mm:ss.SSS", DateUtil.str2Date("yyyy-MM-dd HH:mm:ss SSS", timeStr));
					// System.out.println("timeStr:"+timeStr);
					Timestamp time = Timestamp.valueOf(formatStr);
					record.setTime(time);
				}
				context.write(new Text(matchKey), new Text(JSON.toJSONString(record)));
			
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
	

	}

}
